package app.PN;
import java.util.ArrayList;
import java.util.Date;

import app.model_package.LogModel;


public class ProcessSolutionFactory {

	
	private ArrayList<ArrayList<String>> totalsolution;
	private SolutionNode solution;
	private PetriNet obj;
	public ProcessSolutionFactory(PetriNet studycase){
		obj = studycase;
		solution = new SolutionNode(obj,new ArrayList<String>());
	}
	
	public int containedSolution(ProcessSolutionFactory psf){
		
		this.resolve();
		ArrayList<ArrayList<String>> solution1 = this.solutions();
		
		psf.resolve();
		ArrayList<ArrayList<String>> solution2 = psf.solutions();
		
		boolean total_containment = true;
		boolean partial_containment = false;
		
		for (int i = 0; i<solution2.size();i++){
			boolean aux = this.simpleSolResolver(solution2.get(i));
			total_containment &=aux;
			partial_containment |=aux;
		}
		if (total_containment)
			return 2;
		else if (partial_containment)
			return 1;
		else 
			return 0;
		
	}
	
	public void resolve(){
		solution.findSolution();
	}
	
	public ArrayList<ArrayList<String>> solutions(){
		this.totalsolution = solution.getSolution();
		return totalsolution;
	}
	
	@SuppressWarnings("deprecation")
	public LogModel autosolutions(){
		ArrayList<String> aux;
		ArrayList<ArrayList<String>> amu = this.solutions();
		ArrayList<String> receiver = new ArrayList<String>();
		receiver.add("System");
		LogModel l = new LogModel("exit");
		for (int i = 0 ; i<amu.size();i++){
			
			String _case = "C" + i; 
			
			for (int j = 0;j<amu.get(i).size();j++){
				String aux2 = amu.get(i).get(j);
				l.addEvent(_case, aux2, "agent", receiver, "execute", "", new Date(2012,10,01));
			}
			
		}
		return l;
	}
	
	private boolean simpleSolResolver(ArrayList<String> aux){
		for (int i=0;i<totalsolution.size();i++){
			if (totalsolution.get(i).containsAll(aux))
				return true;
		}
		return false;
	}
	
}
